﻿/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * The Original Code is tyzoh.jp code.
 *
 * The Initial Developer of the Original Code is Kazutaka Kurihara.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** */
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using Qurihara;
using PluginInterface;
//using Microsoft.Office.Core;
using Microsoft.Office.Interop;

namespace KotodamaPlugin.PPT2007DragDropPlugin
{
	public class PPTImport
	{
		//public static bool PPTImportImages(string pptFilePath,Point location,Slide slide,Layout layout,ViewSnapPlugin.ViewSnapPlugin viewSnapP)
		public static bool PPTImportImages(string pptFilePath,Point location,Slide slide,Layout layout,ViewSnapList viewSnapList)
		{
			ArrayList slideImageFileList;
			bool IsPptOK = PPTExportAsImage(pptFilePath,out slideImageFileList);
			if (!IsPptOK) return false;
			if (slideImageFileList.Count > 0)
				InsertSlideImages(slideImageFileList,location,slide,layout,viewSnapList);
				//InsertSlideImages(slideImageFileList,location,slide,layout,viewSnapP);
			return true;
		}

		protected static Microsoft.Office.Interop.PowerPoint.ApplicationClass pptApp;
		protected static Microsoft.Office.Interop.PowerPoint.Presentation ppt;
        protected static Microsoft.Office.Core.MsoTriState tru =
            Microsoft.Office.Core.MsoTriState.msoTrue;
        protected static Microsoft.Office.Core.MsoTriState fal =
            Microsoft.Office.Core.MsoTriState.msoFalse;
		protected static bool PPTExportAsImage(string pptFilePath, out ArrayList imageList)
		{
			//MessageBox.Show("PPTExport開始");

			//PowerPointの起動
			pptApp 
				= new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
			pptApp.Visible = tru;
			pptApp.Activate();
			//MessageBox.Show("PPT起動終了");

			//PowerPointFileのロード
			ppt =
				pptApp.Presentations.Open(
				pptFilePath,
				fal,
				fal,
				tru);
			//MessageBox.Show("PPTロード終了");

			imageList = new ArrayList();
			//Export
			Microsoft.Office.Interop.PowerPoint.Slides slides = ppt.Slides;
			IEnumerator enume = slides.GetEnumerator();
			int i=0;
			while(enume.MoveNext())
			{
				Microsoft.Office.Interop.PowerPoint.Slide slide 
					=(Microsoft.Office.Interop.PowerPoint.Slide)enume.Current;
				i++;
				//slide.Export(workDirPath + "\\" +i.ToString()+".png","PNG",640,480);
				//slide.Export(workDirPath + "\\" +i.ToString()+".png","PNG",1024,768);
				string name = System.IO.Path.GetTempFileName();
				imageList.Add(name);
				slide.Export(name,"PNG",1024,768); // .pngじゃないが大丈夫か？
			}
			ppt.Close();
			//MessageBox.Show("ppt closeしました");
			pptApp.Quit();
			//MessageBox.Show("Export終了しました");
			ppt = null;
			GC.Collect();

			//numOfSlides = i;
			return true;
		}

		public enum Layout
		{
			StraightDown,
			StraightRight,
			FiveColumn,
			FiveZigZag
		}
		//protected static void InsertSlideImages(ArrayList imageList,Point location,Slide slide,Layout layout,ViewSnapPlugin.ViewSnapPlugin viewSnapP)
		protected static void InsertSlideImages(ArrayList imageList,Point location,Slide slide,Layout layout,ViewSnapList viewSnapList)
		{
			float x=0,y=0;
			for(int i=0;i<imageList.Count;i++)
			{
				string slideFileName = (string)imageList[i];
				Bitmap bm = new Bitmap(slideFileName);

				Rectangle r = new Rectangle(location.X+(int)(x*bm.Width),location.Y + (int)(y*bm.Height),bm.Width,bm.Height);
				//slide.View.ScreenToView(ref r);


				if(viewSnapList != null)
				//if(viewSnapP != null)
				{
					ViewSnap vs = new ViewSnap(
						((int)(i+1)).ToString(),
						r,
                        slide.View);
					viewSnapList.Add(vs);

					//slide.View.ViewRectangle = r;
					//viewSnapP.Capture();
				}

				//現在のBitMapエレメントは必ずローカルコピーするんだっけ？
				//PPTファイルは消しちゃうから不安だ。
				//スライドをコピーするときにコピーする仕様。
				Qurihara.QBasic.DrawingUtil.ResizeByRatio(ref r,0.98f);
				slide.createBitmap(5,r,bm);
				//slide.createBitmap(5,r,slideFileName);

				GetPos(layout,ref x,ref y,i+1);
				//y += bm.Height;
				//bm.Dispose();
			}
			GC.Collect();
			for(int i=0;i<imageList.Count;i++)
			{
				string slideFileName = (string)imageList[i];
				try
				{
					System.IO.File.Delete(slideFileName);
				}
				catch(Exception e)
				{
				}
				finally
				{
				}
			}

		}
		protected static void GetPos(Layout layout, ref float x, ref float y,int index)
		{
			switch(layout)
			{
				case Layout.StraightDown:
					y++;
					break;
				case Layout.StraightRight:
					x++;
					break;
				case Layout.FiveColumn:
					x++;
					if(x == 6)
					{
						x =0;
						y++;
					}
					break;
				case Layout.FiveZigZag:
					if (index % 10 < 5) y++;
					else x++;
					break;
				default:
					break;
			}

		}


	}
}